﻿using System;
using QDT.Common;
using QDT.Core.Deals;
using QDT.Core.Projects;

namespace QDT.Core.Assignments
{
    public class Assignment : Entity
    {

        public Assignment()
        {
            CreateTime = DateTime.Now;
            IsDelete = false;
            IsEffect = true;
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// 标的ID
        /// </summary>
        public int ProjectId { get; set; }

        /// <summary>
        /// 标
        /// </summary>
        public virtual Project Project { get; set; }

        /// <summary>
        /// 债权的ID
        /// </summary>
        public int CreditId { get; set; }

        /// <summary>
        /// 转让金额
        /// </summary>
        public decimal Money { get; set; }

        /// <summary>
        /// 当期未得收益
        /// </summary>
        public decimal CurrentEarning { get; set; }

        /// <summary>
        /// 已投资额度
        /// </summary>
        public decimal LoadMoney { get; set; }

        /// <summary>
        /// 手续费率
        /// </summary>
        public double FeeRate { get; set; }

        /// <summary>
        /// 折让率
        /// </summary>
        public double DiscountRate { get; set; }

        /// <summary>
        /// 最小投资额度
        /// </summary>
        public decimal MinLoanMoney { get; set; }

        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsEffect { get; set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        public bool IsDelete { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        public int InnerStatus { get; set; }

        /// <summary>
        /// 债权转让状态
        /// </summary>
        public AssignmentStatus Status
        {
            get { return (AssignmentStatus)InnerStatus; }
            set { InnerStatus = (int)value; }
        }

        /// <summary>
        /// 是否线下
        /// </summary>
        public bool IsOffline { get; set; }

        /// <summary>
        /// 获得投标进度
        /// </summary>
        public double Progress
        {
            get { return Math.Floor(Convert.ToDouble(LoadMoney / Money * 100)); }
        }

        /// <summary>
        /// 可投余额
        /// </summary>
        public decimal NeedMoney
        {
            get { return Money - LoadMoney; }
        }

        /// <summary>
        /// 总折让金额
        /// </summary>
        public decimal TotalDiscount
        {
            get { return Math.Round(Money * (decimal)DiscountRate / 100, 2); }
        }

        /// <summary>
        /// 总手续费
        /// </summary>
        public decimal Fee
        {
            get { return Math.Round(Money * (decimal)FeeRate / 100, 2); }
        }

        /// <summary>
        /// 购买总额
        /// </summary>
        public decimal BuyAmount
        {
            get { return Math.Round(Money + CurrentEarning - TotalDiscount, 2); }
        }

        /// <summary>
        /// 真实赎回金额
        /// </summary>
        public decimal TotalRedeem
        {
            get { return BuyAmount - Fee; }
        }

        public DateTime EndTime
        {
            get { return CreateTime.AddDays(3).Date; }
        }

    }
}
